<?php

namespace App\Http\Controllers\Mini\Estate;

use App\Http\Controllers\Controller;
use App\Services\ComplainsService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class ComplainsController extends Controller
{

    protected $service;

    public function __construct(ComplainsService $complainsService)
    {
        $this->service = $complainsService;
    }

    /**
     * Notes: 添加投诉建议
     * User: lstink
     * Date: 2021/3/5
     * Time: 11:34 上午
     * @param Request $request
     * @return array
     */
    public function store(Request $request): array
    {
        $data = $request->post();
        //数据验证
        $validator = Validator::make($data, [
            'content' => 'required|min:5|max:100',
        ], [
            'content.required'   => '请填写情况描述',
            'content.min'        => '情况描述内容最少5个字符',
            'content.max'        => '情况描述内容最多100个字符',
        ]);

        if ($validator->fails()) {
            $errors = $validator->errors();
            return $this->error($errors->first());
        }

        $info = $this->service->addComplains($data);
        return $info['status'] ? $this->success([]) : $this->error(['msg']);
    }

}
